#include "udpserver.h"

UdpServer::UdpServer(QObject *parent)
    : QObject{parent}
{
    udpSocket = new QUdpSocket(this);
    udpSocket->bind(QHostAddress::Any, 12345); // 绑定到任意地址和端口12345
        connect(udpSocket, &QUdpSocket::readyRead, this, &UdpServer::readPendingDatagrams);
}

void UdpServer::readPendingDatagrams() {
        while (udpSocket->hasPendingDatagrams()) {
            QNetworkDatagram datagram = udpSocket->receiveDatagram();
            processTheDatagram(datagram);
        }
}

void UdpServer::processTheDatagram(const QNetworkDatagram &datagram) {
        QByteArray datagramData = datagram.data();
        qDebug() << "Received datagram:" << datagramData;
        // 在这里处理接收到的数据报
    }
